package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.util.media.MediaType;
import com.baihan.angus.app.game.common.util.media.MediaTypeUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.ArrayList;
import java.util.Iterator;

public class AlbumAdp extends RecyclerView.Adapter {

    public static int MAX_COUNT = 10;

    private OnTieziMediaAdpCallback mOnTieziMediaAdpCallback;
    private ArrayList<String> mMediaPath = new ArrayList<>();
    private Context mContext;

    public AlbumAdp(OnTieziMediaAdpCallback mOnTieziMediaAdpCallback, Context mContext) {
        this.mOnTieziMediaAdpCallback = mOnTieziMediaAdpCallback;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        if (position < mMediaPath.size()) {
            String path = mMediaPath.get(position);
            switch (MediaTypeUtil.getMediaType(path)) {
                case MediaType.IMAGE:
                case MediaType.GIF:
//                    viewHolder.ivVideoPlay.setVisibility(View.INVISIBLE);
                    Glide.with(mContext).load(path).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(viewHolder.ivPhoto);
                    break;
                case MediaType.VIDEO:
//                    viewHolder.ivVideoPlay.setVisibility(View.VISIBLE);
                    viewHolder.ivPhoto.setImageBitmap(ComUtil.getVideoThumbnail(path));
                    break;
            }
            viewHolder.ivClose.setVisibility(View.VISIBLE);
            viewHolder.ivClose.setOnClickListener(v -> {
                remove(position);
                notifyDataSetChanged();
            });
            viewHolder.ivPhoto.setOnClickListener(v -> {
                if (mOnTieziMediaAdpCallback != null) {
                    mOnTieziMediaAdpCallback.preview(position);
                }
            });
        } else {
            viewHolder.ivClose.setVisibility(View.INVISIBLE);
//            viewHolder.ivVideoPlay.setVisibility(View.INVISIBLE);
            viewHolder.ivPhoto.setImageResource(R.mipmap.ic_add_pic);
            viewHolder.ivPhoto.setOnClickListener(v -> {
                if (mOnTieziMediaAdpCallback != null) {
                    mOnTieziMediaAdpCallback.onAdd();
                }
            });
        }
        int screenWidth = AppUtil.getScreenWidth(mContext);
        int itemWidth = (screenWidth - ComUtil.Dp2Px(mContext, 20)) / 3;
        ViewGroup.LayoutParams itemLp = holder.itemView.getLayoutParams();
        itemLp.width = itemWidth;
        holder.itemView.setLayoutParams(itemLp);
    }

    private void remove(int position) {
        Iterator<String> iterator = mMediaPath.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            iterator.next();
            if (index == position) {
                iterator.remove();
                break;
            }
            index++;
        }
        notifyDataSetChanged();
    }

    public int getMediaSize() {
        return mMediaPath.size();
    }

    public ArrayList<String> getSelectedMedias() {
        return mMediaPath;
    }

    public void addAll(ArrayList<String> strings) {
        mMediaPath.addAll(strings);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (mMediaPath.size() < MAX_COUNT)
            return mMediaPath.size() + 1;
        else return MAX_COUNT;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        QMUIRadiusImageView ivPhoto;
        ImageView ivClose;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.ivPhoto);
            ivClose = itemView.findViewById(R.id.ivClose);
        }
    }

    public interface OnTieziMediaAdpCallback {
        void onAdd();

        void preview(int index);
    }
}
